package thread;

public class Test2 {

/**
 * 有三个线程，分别只能打印A，B和C
 * 要求按顺序打印ABC，打印10次
 * 输出示例：
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 * ABC
 */
    public static void main(String[] args) throws InterruptedException {
        Object lock1 = new Object();
        Object lock2 = new Object();
        Object lock3 = new Object();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock1) {
                    try {
                        lock1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("A");
                synchronized (lock2) {
                    lock2.notify();
                }
            }
        },"A");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock2) {
                    try {
                        lock2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("B");
                synchronized (lock3) {
                    lock3.notify();
                }
            }
        },"B");

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (lock3) {
                    try {
                        lock3.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("C");
                synchronized (lock1) {
                    lock1.notify();
                }
            }
        },"C");

        // 启动线程
        t1.start();
        t2.start();
        t3.start();

        // 等待所有线程执行完毕
        Thread.sleep(1000);

        // 唤醒 A
        synchronized (lock1) {
            lock1.notify();
        }
    }


    public static void main1(String[] args) throws InterruptedException {
        Object obj = new Object();

        Thread c = new Thread(() -> {
            Thread cur = Thread.currentThread();
            System.out.print(cur.getName() + " ");
        },"c");

        Thread b = new Thread(() -> {
            try {
                c.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Thread cur = Thread.currentThread();
            System.out.print(cur.getName() + " ");
        },"b");

        Thread a = new Thread(() -> {
            try {
                b.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            Thread cur = Thread.currentThread();
            System.out.print(cur.getName());
        },"a");

        c.start();
        b.start();
        a.start();
//        Thread t1 = new Thread(() -> {
//            synchronized (obj) {
//                for (int i = 0; i < 10; i++) {
//                    System.out.print("A");
//                    try {
//                        obj.wait();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//            }
//        });
//
//        Thread t2 = new Thread(() -> {
//            synchronized (obj) {
//                for (int i = 0; i < 10; i++) {
//                    System.out.print("B");
//                    try {
//                        obj.wait();
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//            }
//        });
//
//        Thread t3 = new Thread(() -> {
//            synchronized (obj) {
//                for (int i = 0; i < 10; i++) {
//                    System.out.println("C");
//                    obj.notifyAll();
//                }
//            }
//        });
//
//        t1.start();
//        t2.start();
//        t3.start();
    }
}
